<template>
	<view>
		<!-- 轮播器 -->
		<swiper class="swiper" :indicator-dots="goodsInfo.pics.length > 1" :autoplay="goodsInfo.pics.length > 1" :interval="3000" :duration="1000" circular>
			<swiper-item class="swiper__item" v-for="(item,index) in goodsInfo.pics" :key="item.pics_id"
				@tap="preview(index)">
				<image class="item__image" :src="item.pics_big ||  defaultImage" mode="scaleToFill">
				</image>
			</swiper-item>
		</swiper>
		<!--  商品信息 -->
		<view class="info">
			<view class="info__price">{{goodsInfo.goods_price}}</view>
			<view class="info__item">
				<view class="item__name">{{goodsInfo.goods_name}}</view>
				<view class="item__collection">
					<view class="iconfont icon-shoucang collection__image"></view>
					<view class="collection__text">收藏</view>
				</view>
			</view>
			<view class="info__sends">快递: 免运费</view>
		</view>
		<!--  富文本对产品进行介绍 -->
		<rich-text :nodes="goods_introduce"></rich-text>
		<!-- 填充高度, 防止底部的导购条将内容遮挡 -->
		<view class="" :style="{height : 50 + safeAreaInsets.bottom + 'px'}"></view>
		<!--  底部导购工具条 -->
		<uni-goods-nav class="goodsNav" :style="{ paddingBottom : safeAreaInsets.bottom + 'px'}" :fill="true"
			:options="options" :button-group="buttonGroup" @click="goBuyLeftItemsClick" @buttonClick="goBuyRightItemsClick" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 商品信息
				goodsInfo: { },
				// 商品介绍
				goods_introduce: '',
				// 底部导购工具条
				options: [{
					icon: 'shop',
					text: '店铺',
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
					}
				],
				// 安全区域信息
				safeAreaInsets: {},
				// 默认图片
				defaultImage: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png'
			}
		},

		onLoad(oprations) {
			let goodsId = oprations.goods_id || '57439'
			// 调用接口获取商品信息
			this.getGoodsDetail(goodsId);
			this.safeAreaInsets = uni.getSystemInfoSync().safeAreaInsets;
		},

		methods: {
			// 调用接口获取商品信息
			getGoodsDetail(id) {
				let params = {
					goods_id: id
				}
				this.$reqGet('/api/public/v1/goods/detail', params).then(res => {
					const code = res.statusCode;
					if (code == 200) {
						this.goodsInfo = res.data.message;
						// 默认给一个
						if (this.goodsInfo.pics.length == 0) {
							this.goodsInfo.pics  = [{pics_big: this.defaultImage}];
						}
						// 使用字符串的 replace() 方法，为 img 标签添加行内的 style 样式，从而解决图片底部空白间隙的问题
						// 使用字符串的 replace() 方法，将 webp 的后缀名替换为 jpg 的后缀名
						this.goods_introduce = res.data.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ')
							.replace(/webp/g, 'jpg');
					}
				})
			},

			// 预览图片
			preview(index) {
				uni.previewImage({
					current: index,
					urls: this.goodsInfo.pics.map(item => item.pics_big),
					complete() {
						console.log('--  预览图片');
					}
				})
			},

			// 左侧点击事件
			goBuyLeftItemsClick(e) {
				uni.showToast({
					title: ` 点击了第${e}个 -- 内容: ${e.content.text}`,
					icon: 'none'
				})
			},

			// 右侧点击事件
			goBuyRightItemsClick(e) {
				if (e.index == 0) {
					//  添加到购物车
					this.options[1].info++;

					/*
					默认将商品添加到本地
					每个商品的信息对象，都包含如下 6 个属性：
					{ goods_id, goods_name, goods_price, goods_count, goods_small_logo, goods_state }
					*/
					let tempdItem = {
						goods_id: this.goodsInfo.goods_id || '',
						goods_name: this.goodsInfo.goods_name || '',
						goods_price: this.goodsInfo.goods_price || '',
						goods_count: 1,
						goods_small_logo: this.goodsInfo.pics[0].pics_big,
						goods_state: 1,
						goods_checked: true
					}
					this.$local_setLocalCarts(this.goodsInfo.goods_id, tempdItem)

					let that = this
					setTimeout(function() {
						uni.switchTab({
							url: '/pages/cart/cart',
						})
					}, 800);
				} else if (e.index == 1) {
					//  立即购买

				}

				uni.showToast({
					title: ` 点击了第${e.index}个 -- 内容: ${e.content.text}`,
					icon: 'none'
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.swiper {
		width: 100%;
		height: 600rpx;

		//background: pink;
		.swiper__item {
			.item__image {
				width: 100%;
				height: 100%;
				border-radius: 10rpx;
			}
		}
	}

	.info {

		display: flex;
		flex-direction: column;
		padding: 0 20rpx;

		.info__price {
			margin: 10rpx 0 0;
			font-size: 40rpx;
			color: #c00000;
			//background: pink;

			display: flex;
			align-items: baseline;

			&::before {
				content: '￥';
				font-size: 30rpx;
				color: #c00000;
				margin-right: 6rpx;
			}
		}

		.info__item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 26rpx;
			color: #333;

			.item__name {
				flex: 1;
				//background: peachpuff;
				border-right: 1rpx solid #EEEDEA;
			}

			.item__collection {
				padding: 0 20rpx;
				display: flex;
				flex-direction: column;
				align-items: center;
				justify-content: center;
				// background: paleturquoise;
				font-size: 26rpx;
				// height: 100rpx;
				padding: 20rpx 30rpx;

				.collection__image {}

				.collection__text {}

			}
		}

		.info__sends {
			margin: 10rpx 0 20rpx;
			font-size: 24rpx;
			color: gray;
		}
	}

	.goodsNav {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
	}
</style>